<template >
  <div class="homeSetUp-container">
    <breadcrumb style="position: absolute;width:100%;"></breadcrumb>
    <div class="content">
      <div class="query">
        <div class="margin_right_20">
          会员卡类型:&nbsp;
          <el-select v-model="all" style="width: 100px;">
            <el-option
              v-for="item in options1"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </div>

        <div class="margin_right_20">
          商品销售状态:&nbsp;
          <el-select v-model="all1" style="width: 100px;">
            <el-option
              v-for="item in options2"
              :key="item.value2"
              :label="item.label"
              :value="item.value2"
            ></el-option>
          </el-select>
        </div>
        <div class="btn">
          <el-button type="primary" icon="el-icon-search" @click="get_goodsList(1)">查询</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="dialogVisible=true">新增</el-button>
        </div>
      </div>
      <div class="table">
        <el-table :data="couponList" style="width: 100%" height="598">
          <el-table-column prop="cardName" label="会员卡类型"></el-table-column>
          <el-table-column prop="goodName" label="商品名称" width="400px"></el-table-column>
          <el-table-column prop="status" label="商品销售状态"></el-table-column>
          <el-table-column prop="sellCount" label="商品已售数量"></el-table-column>
          <el-table-column prop="cardTypeCount" label="会员卡已售（张）"></el-table-column>
          <el-table-column prop="createTime" label="商品创建时间"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="jump_goods_detail(scope.row.goodsId)">详情</el-button>
              <el-button
                v-if="scope.row.status == '下架' ? true:false"
                type="text"
                size="small"
                @click="status_modify(scope.$index,scope.row.id,scope.row.status)"
              >在售</el-button>
              <el-button
                v-else
                type="text"
                size="small"
                @click="status_modify(scope.$index,scope.row.id,scope.row.status)"
              >下架</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          :currentPage="currentPage"
          :page="page"
          :total="total"
          :pageSize="pageSize"
          @change-current="currentChange"
          @change-size="sizeChange"
        ></pagination>
        <el-dialog :visible.sync="dialogVisible" width="20%">
          <el-form label="form" v-model="form" class="demo-form-inline">
            <el-form-item label="商品：" label-width="120px" class="btn1">
              <div
                class="goods_msg"
                style="width: 180px;height: 170px; border: 1px solid #ccc;padding-left:20px;box-sizing: border-box;padding-top: 5px;"
                v-if="goodsId"
              >
                <img :src="goodsImg" alt style="height: 106px;width: 106px;" />
                <p style="color: #333;display: flex;flex-wrap:wrap;margin-top: -10px;">
                  <span style="display: block; width: 100%;" class="ellipsis">{{goodsTitle}}</span>
                </p>
              </div>
              <el-button type="primary" style="width:110px" @click="select_goods">请选择商品</el-button>
            </el-form-item>
            <el-form-item label="会员卡类型: " label-width="120px">
              <el-select v-model="form.cardId" style="width：110px">
                <el-option label="月卡" value="1"></el-option>
                <el-option label="季卡" value="2"></el-option>
                <el-option label="年卡" value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <div class="btn">
              <el-button @click="dialogVisible=false" size="small" style="width:120px">取 消</el-button>
              <el-button type="primary" @click="addCoupon" size="small" style="width:120px">确 定</el-button>
            </div>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>
<script>
import breadcrumb from "../../components/breadcrumb";
import pagination from "../../components/page";
import { positions } from "../../api/getData";
export default {
  components: { breadcrumb, pagination },
  data() {
    return {
      all: "",
      all1: "",
      goodsId: "",
      goodsImg: "",
      goodsPrice: "",
      goodsTitle: "",
      pic: "",
      dialogImageUrl: [],
      isFlag: true,
      active: [],
      bicPig: "",
      imageUrl1: "",
      smallFile: "",
      bigFile: "",
      currentPage: 1,
      total: 0,
      dialogVisible: false,
      dialogVisible1: false,
      couponList: [],
      options1: [
        {
          value: "",
          label: "所有"
        },
        {
          value: "1",
          label: "月卡"
        },
        {
          value: "2",
          label: "季卡"
        },
        {
          value: "3",
          label: "年卡"
        }
      ],
      options2: [
        {
          value2: "",
          label: "所有"
        },
        {
          value2: "0",
          label: "在售"
        },
        {
          value2: "1",
          label: "下架"
        }
      ],
      page: 1,
      pageSize: 10,
      dialogFormVisible: false,
      form: [],
      formLabelWidth: "120px"
    };
  },
  props: ["positionsId"],
  created() {
    if (localStorage.getItem("pageMsg")) {
      let pageMsg = JSON.parse(localStorage.getItem("pageMsg"));
      this.all = pageMsg.all;
      this.all1 = pageMsg.all1;
      this.currentPage = pageMsg.currentPage;
      this.pageSize = pageMsg.pageSize;
      localStorage.removeItem("pageMsg");
    }

    this.get_goodsList();
    this.goodsId = localStorage.getItem("id") || "";
    localStorage.removeItem("id");
    if (this.goodsId) {
      this.dialogVisible = true;
      this.get_goodsDeatil();
    }
  },
  methods: {
    addCoupon() {
      if (!this.goodsId) {
        return this.$message({
          type: "warning",
          message: "请选择活动类型为开通会员赠送的商品"
        });
      }
      if (!this.form.cardId) {
        return this.$message({
          type: "warning",
          message: "请选择会员卡类型"
        });
      }
      let data = {
        cardId: this.form.cardId,
        goodsId: this.goodsId
      };
      this.$axios.post("/member/set/save", data).then(result => {
        if (result.data.status == 0) {
          if (result.data.data.data == 1) {
            this.get_goodsList();
            this.$message({
              type: "success",
              message: "添加成功"
            });
          } else {
            this.$message({
              type: "warning",
              message: "操作失败"
            });
          }
        }
      });

      this.dialogVisible = false;
    },
    status_modify(i, id, status) {
      this.$confirm("此操作将修改状态, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          var goodsStatus = "";
          if (status == "在售") {
            goodsStatus = 1;
          } else {
            goodsStatus = 0;
          }
          let data = {
            cardId: this.couponList[i].cardId,
            goodsId: this.couponList[i].goodsId,
            status: goodsStatus,
            id
          };

          this.$axios.post("/member/set/save", data).then(result => {
            if (result.data.status == 0) {
              if (result.data.data.data == 1) {
                if (status == "下架") {
                  this.$message({
                    type: "success",
                    message: "上架成功"
                  });
                } else {
                  this.$message({
                    type: "success",
                    message: "下架成功"
                  });
                }

                this.couponList.forEach(item => {
                  if (item.id == id) {
                    item.saleStatus =
                      item.saleStatus == "在售" ? "下架" : "在售";
                  }
                });
                this.get_goodsList();
              } else {
                this.$message({
                  type: "warning",
                  message: "操作失败"
                });
              }
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消修改"
          });
        });
    },
    sizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.get_goodsList();
    },
    currentChange(val) {
      this.currentPage = val;
      this.get_goodsList();
    },
    jump_goods_detail(id) {
      localStorage.setItem("path", JSON.stringify(this.$route.path));

      localStorage.setItem(
        "pageMsg",
        JSON.stringify({
          all: this.all,
          all1: this.all1,
          currentPage: this.currentPage,
          pageSize: this.pageSize
        })
      );

      this.$router.push("goodsDetail?id=" + id);
    },
    get_goodsList(currentPage) {
      if (currentPage) {
        this.currentPage = currentPage;
      }
      let data = {
        current: this.currentPage,
        size: this.pageSize
      };
      // cardStatus: this.all,
      //   sellStatus: this.all1
      this.$axios
        .post(
          "/member/set/list?cardStatus=" +
            this.all +
            "&sellStatus=" +
            this.all1,
          data
        )
        .then(res => {
          if (res.data.status == 0 && res.data.data.records.length != 0) {
            this.couponList = res.data.data.records;
            this.couponList.forEach(item => {
              if (item.id) {
                item.status = item.status == 0 ? "在售" : "下架";
              }
            });
            this.total = res.data.data.total;
            this.page = Math.ceil(this.total / this.pageSize);
          }
        });
    },

    select_goods() {
      localStorage.setItem(
        "formData",
        JSON.stringify({
          form: this.form
        })
      );
      this.$router.push("/select_goods");
    },
    get_goodsDeatil() {
      this.$axios
        .post("/web_goods/get_one", "id=" + this.goodsId)
        .then(result => {
          if (result.data.status == 0) {
            let data = result.data.data.goods;
            this.goodsImg = data.pictureUrl;
            this.goodsPrice = data.price;
            this.goodsTitle = data.name;
          }
        });
    }
  }
};
</script>


<style lang="less" >
.homeSetUp-container {
  @import "../../style/table.less";
  @import "../../style/content.less";
  display: flex;
  flex-flow: column;
  position: relative;
  height: 100%;
  .el-table .cell {
    white-space: pre-line !important;
  }
  .content {
    .query {
      .start_time {
        display: flex;
        align-items: center;
      }
      .end_time {
        display: flex;
        align-items: center;
      }
      .btn {
        .el-button {
          width: 70px;
        }
      }
    }
  }
}
</style>

